package com.pl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.SysUser;
import com.pl.service.SysUserService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(SysUser)表控制层
 *
 * @author makejava
 * @since 2024-04-24 17:26:06
 */
@RestController
@RequestMapping("/api/sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录
     *
     * @param sysUser
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public R login(SysUser sysUser, HttpServletRequest request, HttpSession session) {
        SysUser user = sysUserService.login(sysUser, request, session);
        if (user != null) {
            return R.ok(user);
        }
        return R.fail("用户名或者密码错误");
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);

        // 3.按照条件查询
        List<SysUser> list = sysUserService.queryByPage(map);

        // 4 返回R 内部有pageinfo
        if (list != null && list.size() > 0) { // 返回成功数据

            PageInfo<SysUser> pageInfo = new PageInfo<>(list);// 目的是的到总行数 总页数

            return R.ok(pageInfo);
        }
        return R.fail("查询数据为空");
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {

        boolean result = sysUserService.deleteBatch(list);
            if (result){
                return R.ok();
            }
            return R.fail("批量删除失败");

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.sysUserService.queryById(id));
    }


    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(SysUser sysUser,HttpSession session) {
        boolean result = this.sysUserService.insert(sysUser, session);
       if (result){
           return R.ok();
       }
        return R.fail("插入失败");
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(SysUser sysUser) {
        boolean result = this.sysUserService.update(sysUser);
        if (result){
            return R.ok();
        }
        return R.fail("编辑失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") int id) {
        return R.ok(this.sysUserService.deleteById(id));
    }

    /**
     * 导入文件
     * @param file
     * @return
     */
    @RequestMapping("/importSysUserFile")
    public String importProductFile(@Param("file") MultipartFile file) {
        return sysUserService.importSysUserFile(file);
    }

    /**
     * 导出文件
     * @param response
     */
    @RequestMapping("/exportSysUserFile")
    public void exportProductFile(HttpServletResponse response) {
        sysUserService.exportSysUserFile(response);
    }

    /**
     * 查询全部数据
     */

    @RequestMapping("/finAll")
    public R finAll(){
        boolean result=sysUserService.finAll();
        if (result){
            return R.ok();
        }
        return R.fail("查询失败");

    }
}

